package io.github.wx.common.util;

import com.jfinal.kit.LogKit;
import com.jfinal.template.IStringSource;

import java.io.*;

/**
 * 下载jfinal 3.1 release 包，替换
 * com.jfinal.template.FileStringSource
 */
public class FileStringSource implements IStringSource {
    private String finalFileName;
    private String fileName;
    private String encoding;

    public FileStringSource(String baseTemplatePath, String fileName, String encoding) {
        this.finalFileName = this.buildFinalFileName(baseTemplatePath, fileName);
        this.fileName = fileName;
        this.encoding = encoding;
    }

    public FileStringSource(String baseTemplatePath, String fileName) {
        this(baseTemplatePath, fileName, "UTF-8");
    }

    public String getKey() {
        return this.fileName;
    }

    public StringBuilder getContent() {
        return loadFile(this.finalFileName, this.encoding);
    }

    public String getEncoding() {
        return this.encoding;
    }

    public String getFinalFileName() {
        return this.finalFileName;
    }

    private String buildFinalFileName(String baseTemplatePath, String fileName) {
        char firstChar = fileName.charAt(0);
        String finalFileName;
        if (firstChar != 47 && firstChar != 92) {
            finalFileName = baseTemplatePath + File.separator + fileName;
        } else {
            finalFileName = baseTemplatePath + fileName;
        }

        return finalFileName;
    }

    public static StringBuilder loadFile(String fileName, String encoding) {
        if (fileName.indexOf("classpath:") == 0) {
            fileName = fileName.replace("classpath:/", "");
            StringBuilder out = new StringBuilder();
            InputStream inputStream = com.jfinal.template.FileStringSource.class.getClassLoader().getResourceAsStream(fileName);
            byte[] b = new byte[4096];
            try {
                for (int n; (n = inputStream.read(b)) != -1; ) {
                    out.append(new String(b, 0, n));
                }
            } catch (IOException e) {
                throw new RuntimeException("Error loading sql file.", e);
            } finally {
                if (inputStream != null) try {
                    inputStream.close();
                } catch (IOException e) {
                    LogKit.error(e.getMessage(), e);
                }
            }
            return out;
        } else {
            File file = new File(fileName);
            if (!file.exists()) {
                throw new RuntimeException("File not found : " + fileName);
            } else {
                StringBuilder ret = new StringBuilder((int) file.length() + 3);
                BufferedReader br = null;

                StringBuilder var6;
                try {
                    br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
                    String e = br.readLine();
                    if (e != null) {
                        ret.append(e);

                        while ((e = br.readLine()) != null) {
                            ret.append("\n").append(e);
                        }

                        var6 = ret;
                        return var6;
                    }

                    var6 = ret;
                } catch (Exception var18) {
                    throw new RuntimeException(var18);
                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (IOException var16) {
                            LogKit.error(var16.getMessage(), var16);
                        }
                    }

                }

                return var6;
            }
        }
    }
}
